package com.beadhouse.nurlevel.web;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.beadhouse.common.dto.JSONResponse;
import com.beadhouse.common.dto.QueryResult;
import com.beadhouse.entity.NurContentEntity;
import com.beadhouse.entity.NurLevelAndNurContentEntity;
import com.beadhouse.nurlevel.ao.AddNurLevelContentAO;
import com.beadhouse.nurlevel.ao.UpdateNurLevelContentAO;
import com.beadhouse.nurlevel.service.NurLevelContentService;

@Controller
@RequestMapping("/api/nurlevel/content")
public class NurLevelContentController {
	@Autowired
	NurLevelContentService nlcService;
	
	@GetMapping("/add")
	public String addNurLevelContent(AddNurLevelContentAO ao) throws Exception {
		nlcService.addNurLevelContent(ao);
		int levelId=ao.getLevelId();
		System.out.println(levelId);
		return "forward:/api/nurlevel/content/select?id="+levelId;
	}
	
	@GetMapping("/delete")
	public	String delete(int id,int levelId) throws Exception {
		nlcService.deleteNurLevelContent(id);
		return "forward:/api/nurlevel/content/select?id="+levelId;
	}
	
	@GetMapping("/update")
	public String updateNurLevelContent(UpdateNurLevelContentAO ao) throws Exception {
		nlcService.updateNurLevelContent(ao);
		return "forward:/api/nurlevel/content/select?id="+ao.getLevelId();
	}
	
	@GetMapping("/selectone")
	@ResponseBody
	public JSONResponse selectOneNurLevelcontent(int levelId,int contentId)throws Exception {
		NurLevelAndNurContentEntity	r=nlcService.selectOne(levelId, contentId);
		System.out.println(r);
		return JSONResponse.ok(r);
	}
	
	@GetMapping("/selectall")
	@ResponseBody
	public JSONResponse selectAllContent() throws Exception {
		List<NurContentEntity> 	r=nlcService.selectAllContent();
		return JSONResponse.ok(r);
	}
	
	@GetMapping("/select")
	public ModelAndView selectNurLevelContentList(Integer id) throws Exception {
		QueryResult<NurLevelAndNurContentEntity> s=nlcService.selectNurLevelContent(id);
		ModelAndView mav=new ModelAndView();
		mav.setViewName("/nurlevel/nurlevelcontent");
		mav.addObject("data",s);
		return mav;
	}
}
